<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of tfFormCheckBox
 *
 * @author waterfrog
 */
if(!defined('TinyFW')) exit('Access denied!');

class tfFormRadio extends tfFormElement {

    private $_options = array();
    private $_delimeter = "<br />";
    //default columns is 2
    private $_columns = 0;

    public function  __construct( $name='', $id='', $caption='', $value=null, $delimeter='<br />', $columns = 2, $attr_arr='') {

        $this->setName($name);
        $this->setCaption($caption);
        $this->setId($id);
        if(isset ($value)) {
            $this->setValue($value);
        }
        $this->setDelimeter($delimeter);
        $this->setColumns($columns);
        $this->setAttr($attr_arr);

    }

    public function setColumns($columns) {
        if(isset($columns) and is_int($columns)) {
            $this->_columns = $columns;
        }
    }

    public function getColumns() {
        return $this->_columns;
    }

    public function addOption($value,$name='') {
        if($name != '') {
            $this->_options[$value] = $name;
        } else {
            $this->_options[$value] = $value;
        }
    }

    public function addOptionArray($options) {
        if(is_array($options)) {
            foreach($options as $k => $v) {
                $this->addOption($k, $v);
            }
        }
    }

    public function getOptions(){
        return $this->_options;
    }

    public function getDelimeter(){
        return $this->_delimeter;
    }

    public function setDelimeter($delimeter) {
        $this->_delimeter = $delimeter;
    }

    public function render() {
        $ret = '';
        $count = 0;
        foreach($this->_options as $k => $v) {
            $ret .= "<input type='radio' name='" . $this->getName() . "' id='" . $this->getId() . $k . "' value='" . $k . "' ";
            if($k == $this->getValue()) {
                $ret .= " checked='checked' ";
            }
            $ret .= "/><label for='" . $this->getId().$k . "' >".$v."</label>&nbsp;";
            $count ++;
            if( $count % $this->getColumns() == 0 ) {
                $ret .= $this->getDelimeter();
            }

        }

      return $ret;
    }


}
?>
